$(function() {
//Declare variable
var 	tips = $( ".validateTips" ),
		button_name="",
		url="a_return_view.php", 
 		ID="",
		mode="",
		colspan=0,
		branchID="",
		rowSelect="";

		ID=$("#val").val();
		mode=$("#mode").val();
		branchID=$("#branch").val();
		
		if ((mode=='c') || (mode=='cl')){
			colspan=9;
		}else{
			colspan=7;
		}
		//Load First page
		LoadData(1, ID);
	
		
		//Show Loading bar			
		function showLoader(){
			$( "#resultData tbody:first" ).prepend( "<tr>"
						+"<td colspan="+ colspan +" class='search-background'>"
						+"<label><img src='./images/ajax-loader.gif'/></label>"
						+"</td>"
						+"</tr>" ); 
			 $('.search-background').fadeIn(900,0);
		}
		//Hide Loading bar			
		function hideLoader(){
			$('.search-background').fadeOut(300);
			$( "#resultData tbody tr" ).remove();
		};
		
		//b=false , don't gennerate paging
		function LoadData(page,id){
			$( "#resultData tbody tr" ).remove();
			
			//Show loading
			showLoader();
			//set criteria
			var dataset={'m':'view','id':id,'page':page,'type':mode};

			$.post(url,dataset,function(data){
				//hide loading
				// alert(data);
				hideLoader();
				var obj=eval("(" + data + ")");
				var ar=eval("(" + obj['header'] + ")");
			
				$("#iCode").attr('innerHTML',(ar['Code']));
				$("#iName").attr('innerHTML',(ar['BranchName']));
				$("#iStatus").attr('innerHTML',(ar['Status']));
				$("#iRemark").attr('innerHTML',(ar['Remark']));
				//CreateDate
				$("#dtCreatedDate").attr('innerHTML',(ar['CreatedDate']));
				$("#lblCreatedBy").attr('innerHTML',(ar['CreatedBy']));
		 
				if (ar['iStatus']==2){
					//SendDate
					$("#iRowReturnDate").show();
					$("#dtReDate").attr('innerHTML',(ar['ReturnDate']));
					$("#lblReBy").attr('innerHTML',(ar['ReturnBy']));
				}else{
					$("#iRowReturnDate").hide();
				}
				if (ar['iStatus']==3){
					//Return Date
					$("#iRowReturnDate").show();
					$("#dtReDate").attr('innerHTML',(ar['ReturnDate']));
					$("#lblReBy").attr('innerHTML',(ar['ReturnBy']));
					//ClosedDate
					$("#iRowCloseDate").show();
					$("#dtClosedDate").attr('innerHTML',(ar['UpdatedDate']));
					$("#lblClosedBy").attr('innerHTML',(ar['UpdatedBy']));
				}else{
					$("#iRowReturnDate").hide();
					$("#iRowCloseDate").hide();
				}
 
				$( "#resultData tbody tr" ).remove();
				
				$( "#resultData tbody" ).append(obj['data']);
				
				//set row 
				$("#rowCount").attr('innerHTML',obj['rows']);
				
				if (mode=='v'){
					//set page
						var p="<ul>";
						for (var i=parseInt(obj['totalpage']);i >0 ;i--){
							p = p +'<li id="'+i+'">'+i+'</li>';
						}
						p = p+"</ul>";
						$("#paging_button ul").remove();
						$("#paging_button").append(p);
						 
					 
					$("#"+page).css({'background-color' : '#006699'});
				}
			
			});
			
		}
		//set textbox to input only numeric
		$("#txtAmt").live('click',function(){
			//alert('dd');
				$(this).select();
				$(this).numeric();
		})
		$("#txtAmt").live('keyup',function(){
			var Amt = new Number( $(this).parent().parent().find("td").eq(7).html() );
			//alert(Amt);
			if ( parseInt($("#txtAmt").val()) > Amt ){
				$("#txtAmt").val(Amt);
			}
		});
	
 
		//Close Dialog
		$( "#dialog:ui-dialog" ).dialog( "destroy" );
		
		//Confirm Dialog
		$( "#dialog-confirm" ).dialog({
			resizable: false,
			height:160,
			autoOpen: false,
			modal: true
		});
		// Confirm return product 
		$("#btnConfirm").click(function(){
			var arlist=[];
			var return_id_detail =0;
			var p_id=0;
			var amt=0;
			var type=0;

		
			$("#dialog-confirm").dialog("open");
			$("#dialog-confirm").dialog("option","buttons",	{
					"Confirm": function() {
					
					// Get value from table
					$('#resultData tbody tr').each(function() {
						// return id detail
						return_id_detail = $(this).find("td").eq(1).html();    
						 p_id= $(this).find("td").eq(2).html();    
						// return amount
						 amt = $(this).find("input[name=txtAmt]").val();   
						 // return type
						 type= $(this).find("select[name=type_conf]").val();    
						//	alert(type);	
							
						if (isNaN(amt) || amt==''){
							amt=0
						}
						arlist.push({'id': return_id_detail ,'pid':p_id, 'amt':amt ,'type':type});
					 });
					
					$.ajax({
							type: 'POST',
							cache: false,
							url: url,
							data: {"m":"confirm","re_id":ID,"var":arlist,"branch":branchID},
							success: function(data){
							//	alert(data);
								alert( "Completed!!") ;
							}
						});
					
					$( this ).dialog( "close" );
					history.back(-1);
				},
				Cancel: function() {
						$( this ).dialog( "close" );
				}
			});
		});
	
		// Send to HO
		$("#btnCheck").live('click' ,function(){
	 		$("#dialog-confirm").dialog("open");
			$("#dialog-confirm").dialog("option","buttons",	{
				"Return": function() {
					$.ajax({
							type: 'POST',
							cache: false,
							url: url,
							data: {"m":"send","id":ID},
							success: function(data){
									alert( "Completed!!") ;
							}
						});
					
					$( this ).dialog( "close" );
					history.back(-1);
				},
				Cancel: function() {
						$( this ).dialog( "close" );
				}
			});
	});
	
	$("#btnBack").live('click',function(){
		//alert('d');
		history.back(-1);
	});
	
	$("#paging_button li").live('click',function(){
		$("#paging_button li").css({'background-color' : ''});
 		LoadData(this.id,ID);

	});
});
	